import { AsyncStorage } from 'react-native';

import {
  LOGIN,
  LOGOUT,
  SET_USER_INFO,
  TOKEN
} from '../types';

export function login() {
  return {
    type: LOGIN,
    needBack: false
  }
}

export function loginAndGoBack() {
  return {
    type: LOGIN,
    needBack: true
  }
}

export function logout() {
  return async dispatch => {
    try {
      await AsyncStorage.removeItem(TOKEN);
      return dispatch({
        type: LOGOUT
      });
    } catch (error) {
      throw error;
    }
  }
}

export function setUserInfo(info) {
  return {
    type: SET_USER_INFO,
    info
  }
}
